<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.9
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############

/**
 * Create a new element facilitating the addition of checkboxes to the form
 * @author Anthony Gallon
 * @package Antz
 */


/**
 * Create a new element facilitating the addition of checkboxes to the form
 * @author Anthony Gallon
 * @package Antz
 */
class JFX_Form_Components_CheckboxGroup extends JFX_Form_Component
{
    /**
     * Stack containing all checkbox elements
     * @var array $components
     */
    protected $components = array();
    /**
     * Define the type of object
     * @var string $type
     */
    protected $type = 'checkboxGroup';

    /**
     * Add a checkbox to the form
     * @param string $name
     * @param string $label
     * @param string $value
     * @param bool $checked
     * @param string $aftertext
     * @return object JFX_Form_Components_Checkbox
     */
    public function add($name, $label, $value, $checked=false, $aftertext=''){
        $checked = (bool) $checked;
        $this->name = $name;
        $this->type = 'checkboxGroup';
        $this->components[] = new JFX_Form_Components_Checkbox($name, $label, $value, $checked, $aftertext);
        $obj = $this->components[(count($this->components)-1)];
        $obj->id = $this->id.'_'.(count($this->components)-1);
        return $obj;
    }

    /**
     * Return all elements as HTML string
     * @return string $html
     */
    public function render(){
        if($this->isXhtml) $closeTag = ' />';
        else $closeTag = '>';
        $returnString = "\n".'<div class="checkboxGroup" id="'.$this->getId().'"'.'>';
        foreach($this->components as $component){
            $returnString .= "\n  ".$component->render();
        };
        $returnString .= "\n".'</div>';
        if($this->wrap) $returnString = $this->wrap($returnString);
        return $returnString;
    }

    /**
     * Add a checkbox which toggles the checking of all checkboxes
     */
    public function addCheckAll(){
        $this->components[] = new JFX_Form_Components_LongTag('p', '<a href="javascript: JFXForm.toggleCheckAll(\''.$this->getId().'\')">Check / Uncheck All</a>');
    }
}
